/**
 * 报关单
 */
/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增
			{
				var selectedDatas = [];
				//回填数据的代码
				var gridDatas = $scope.subGrid.sonlist;
				//子列表去重，根据报关信息子件表id
				for (var i = 0; i < gridDatas.length; i++) {
					var row = gridDatas[i];
					if(i==0){
						selectedDatas.push(row);
					}else {
						var isAdded = false;
						for(var j = 0; j < selectedDatas.length; j++){
							if(selectedDatas[j].decCustomSubId == gridDatas[i].decCustomSubId){
								isAdded = true;
							}
						}
						if(!isAdded){
							selectedDatas.push(row);
						}
					}

				}
				var listUkey = [];
				for(var i=0;i<selectedDatas.length;i++){
					var data = selectedDatas[i];
					listUkey.push(data.decCustomSubId);
				}
				console.log("------listUkey------",listUkey);
				$scope.showModalCheckBox('TL_DecCustom_By_CustomsDeclaration', 'id', listUkey);

				break;
			}
			case 'event-to-delete': // 删除选中数据
			{
				$scope.subGrid.deleteBatch();

				$http.post('/api/inter/tlCustomsDeclaration/getInvoiceDeductionAddInvoice', {}, {params: { subData:JSON.stringify($scope.subGrid.sonlist)}})
					.success(function (res) {
						console.log('aaa',res)
						if (res.code == 0) {
							$scope.subGrid.sonlist = res.resList;
						}
					})
					.error(function(result){
						layer.close();
						layer.msg("操作失败，请稍后再试！", {icon: 2});
					});
				break;
			}


		};
	}

}


app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){

	/*
	 * 数字框变动
	 */
	$scope.subGrid.numberChange = function(row, field) {
		var index = row.index;
		if (field == 'offsetNum') {//监听本次报关数量

			if(t.c.sonlist[index]['customsQuantity']) {
				var customsQuantity = parseInt($scope.subGrid.sonlist[index]['customsQuantity']);//本次报关数量
				var invoiceSurplusNum = parseInt($scope.subGrid.sonlist[index]['invoiceSurplusNum']);//发票剩余报关数量

				if (Number(customsQuantity)> Number(invoiceSurplusNum)){
					layer.msg("本次报关数量不允许大于发票剩余报关数量!");
					$scope.subGrid.sonlist[index]['customsQuantity'] = 0;
					return;
				}

				//商品总重 = 商品单重*本次报关数量
				var goodsTotalWeight = 0;
				var singleWeight = $scope.subGrid.sonlist[index]['singleWeight'];//商品单重
				if(singleWeight && customsQuantity){
					goodsTotalWeight = (parseFloat(singleWeight) * parseFloat(customsQuantity)).toFixed(2);
				}
				$scope.subGrid.sonlist[index]['goodsTotalWeight'] = goodsTotalWeight;

				//销售单价 = 子件采购单价*1.02
				var saleUnitPrice = 0;
				var subPurchasePrice = $scope.subGrid.sonlist[index]['subPurchasePrice'];//子件采购单价
				if(subPurchasePrice){
					saleUnitPrice = (parseFloat(subPurchasePrice) * 0.12).toFixed(2);
				}
				$scope.subGrid.sonlist[index]['saleUnitPrice'] = saleUnitPrice;

				//销售总价 = 销售单价*本次报关数量
				var saleTotalPrice = 0;
				if(saleUnitPrice && customsQuantity){
					saleTotalPrice = (parseFloat(saleUnitPrice) * parseFloat(customsQuantity)).toFixed(2);
				}
				$scope.subGrid.sonlist[index]['saleTotalPrice'] = saleTotalPrice;

			}

		}
	}
}



/*
* 子列表控制器
*/
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	//多选框回调
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {
		for (var i = 0; i < rowsSelected.length; i++) {
			var row = rowsSelected[i];
			var isAdd = true; // 根据现有存在，是否加新行
			for (var j = 0; j < $scope.subGrid.sonlist.length; j++) {
				if ($scope.subGrid.sonlist[j].decCustomSubId == row.decCustomSubId) {
					isAdd = false;
				}
			}
			if (isAdd) {
				row.id = '';
				//本次报关数量 = 子件装箱数量-已报关数量
				row.customsQuantity = parseInt(row.subBoxNum) - parseInt(row.numDecCustom);

				//商品总重 = 商品单重*本次报关数量
				var goodsTotalWeight = 0;
				if(row.singleWeight && row.customsQuantity){
					goodsTotalWeight = (parseFloat(row.singleWeight) * parseFloat(row.customsQuantity)).toFixed(2);
				}
				row.goodsTotalWeight = goodsTotalWeight;

				//销售单价 = 子件采购单价*1.02
				var saleUnitPrice = 0;
				if(row.subPurchasePrice){
					saleUnitPrice = (parseFloat(row.subPurchasePrice) * 0.12).toFixed(2);
				}
				row.saleUnitPrice = saleUnitPrice;

				//销售总价 = 销售单价*本次报关数量
				var saleTotalPrice = 0;
				if(saleUnitPrice && row.customsQuantity){
					saleTotalPrice = (parseFloat(saleUnitPrice) * parseFloat(row.customsQuantity)).toFixed(2);
				}
				row.saleTotalPrice = saleTotalPrice;

				$scope.subGrid.sonlist.push(row);

			}
		}

		$http.post('/api/inter/tlCustomsDeclaration/getInvoiceDeductionAddInvoice', {}, {params: { subData:JSON.stringify($scope.subGrid.sonlist)}})
			.success(function (res) {
				console.log('aaa',res)
				if (res.code == 0) {
					$scope.subGrid.sonlist = res.resList;
				}
			})
			.error(function(result){
				layer.close();
				layer.msg("操作失败，请稍后再试！", {icon: 2});
			});

	}
}]);
